/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.swirrel.installer;

import java.awt.Component;
import java.awt.List;
import java.awt.event.ItemListener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.swirrel.annotation.ItemStateChanged;
import org.swirrel.listener.SwirrelItemListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class ItemListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {

    public ItemListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(ItemStateChanged.class),
                Arrays.<Class<?>>asList(List.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
       ItemListener[] itemListeners = ((List) obj).getItemListeners();
        for (ItemListener il : itemListeners) {
            if (il instanceof SwirrelItemListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, java.util.List<MethodCall> methodCalls) throws IllegalAccessException {
        Object obj = field.get(comp);
        ItemListener al = new SwirrelItemListener(methodCalls.get(0));
         ((List) obj).addItemListener(al);
    }
}
